﻿/*
	Copyright 2010 Intuitive Solutions

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	   http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

using System;
using System.Collections.Generic;

namespace Metamorph.Core.Bpm.Activities
{
	public class ActivityFactory
	{
		private static ActivityFactory afInstance;

		private ActivityFactory()
		{
		}

		public static ActivityFactory Instance
		{
			get
			{
				if (afInstance == null)
				{
					afInstance = new ActivityFactory();
				}

				return afInstance;
			}
		}

		public ITask CreateTask(string sTaskName, string sActionName, string sActivityType, Dictionary<string, string> dProperties)
		{
			ITask tTask = null;

			if (string.IsNullOrEmpty(sActionName))
			{
				sActionName = sTaskName.Replace(" ", "") + "Action";
			}

			switch (sActivityType.ToLower())
			{
				case "loop":
					tTask = new LoopTask(sTaskName);

					SetLoopProperties((ILoopActivity)tTask, dProperties);

					break;
				case "normal":
					tTask = new Task(sTaskName);
					break;
				default:
					break;
			}

			tTask.ActionName = sActionName;

			return tTask;
		}

		public ISubprocess CreateSubprocess(string sSubprocessName, string sActivityType, Dictionary<string, string> dProperties)
		{
			ISubprocess sSubprocess = null;

			switch (sActivityType.ToLower())
			{
				case "loop":
					sSubprocess = new LoopSubprocess(sSubprocessName);

					SetLoopProperties((ILoopActivity)sSubprocess, dProperties);

					break;
				case "normal":
					sSubprocess = new Subprocess(sSubprocessName);
					break;
				default:
					break;
			}

			return sSubprocess;
		}

		public ISubprocess CreateSubprocessReference(ISubprocess sReferencedSubprocess, string sActivityType, Dictionary<string, string> dProperties)
		{
			ISubprocess sSubprocess = null;

			switch (sActivityType.ToLower())
			{
				case "loop":
					sSubprocess = new LoopSubprocessReference(sReferencedSubprocess);

					SetLoopProperties((ILoopActivity)sSubprocess, dProperties);

					break;
				case "normal":
					sSubprocess = new SubprocessReference(sReferencedSubprocess);
					break;
				default:
					break;
			}

			return sSubprocess;
		}

		public void SetLoopProperties(ILoopActivity laLoopActivity, Dictionary<string, string> dProperties)
		{
			if (dProperties != null)
			{
				if (dProperties.ContainsKey("ExpressionCondition"))
				{
					laLoopActivity.ExpressionCondition = dProperties["ExpressionCondition"];
				}

				if (dProperties.ContainsKey("MaximumCondition"))
				{
					laLoopActivity.MaximumCondition = int.Parse(dProperties["MaximumCondition"]);
				}
			}
		}
	}
}